//
//  SCSearchView.swift
//  weibo
//
//  Created by 阿城 on 15/12/5.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
@IBDesignable
class SCSearchView: UIView {

    @IBOutlet weak var searchText: UITextField!
    @IBOutlet weak var rightCont: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    class func getSearchView() -> SCSearchView {
        
        let searchView = NSBundle.mainBundle().loadNibNamed("SCSearchView", owner: nil, options: nil).first as! SCSearchView
        
        return searchView
    }

    override func awakeFromNib() {
        
        changeLeftView("tabbar_discover")
        
        //        let btn = UIButton(type: .ContactAdd)
        //        btn.addTarget(self, action: "add", forControlEvents: .TouchUpInside)
        //        addSubview(btn)
        
    }
    
//    @objc private func add(){
//        printLog("add")
//    }
    
    @IBAction private func cancelClick(sender: UIButton) {
        changeLeftView("tabbar_discover")
        rightCont.constant = 0
        searchText.resignFirstResponder()
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        
    }
    
    
    @IBAction private func textBeginEditing(sender: AnyObject) {
//        printLog("textBeginEditing")
        changeLeftView("tabbar_discover_selected")
        rightCont.constant = cancelBtn.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    private func changeLeftView(imgName: String){
        
        let leftImgView = UIImageView(image: UIImage(named: imgName))
        leftImgView.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
        leftImgView.contentMode = .Center
        searchText.leftViewMode = .Always
        searchText.leftView = leftImgView
        
    }
    
}
